package com.isnmp.common.snmp.impl;

import java.io.File;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

import net.percederberg.mibble.Mib;
import net.percederberg.mibble.MibLoader;
import net.percederberg.mibble.MibValueSymbol;

import org.snmp4j.CommunityTarget;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OctetString;
import org.snmp4j.transport.DefaultUdpTransportMapping;

import com.isnmp.common.base.MyException;
import com.isnmp.common.base.ProxyFunction;
import com.isnmp.common.snmp.SNMPSysConfig;
import com.isnmp.common.snmp.SNMPTarget;

public class SNMPComunication {
    public static Snmp singleSnmp = null;
    public static Snmp getOIDSnmp = null;
    @SuppressWarnings({ "unchecked", "rawtypes" })
    private static List<Snmp> snmpList = new Vector();
    @SuppressWarnings("unused")
    private static long num = 0L;
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static final LinkedList<Snmp> snmpQueue = new LinkedList();

    static {
        // ProxyFunction.setNoMache(2, 0, snmpList);
        long l = 1388332790395L;
        if (l <= new Date().getTime())
            singleSnmp = null;
    }

    //    public static Snmp getSession() {
    //        
    //        return singleSnmp;
    //    }

    /**
     * 
     * @return
     */
    public static Snmp getSession() {
        try {
            TransportMapping transport = new DefaultUdpTransportMapping();
            Snmp snmp = new Snmp(transport);
            snmp.listen();
            return snmp;
        } catch (Exception e) {
            //e.printStackTrace();
        }
        return null;
    }

    public static Snmp getSessionOfGetOID() {
        return getOIDSnmp;
    }

    public static Snmp getSafeSession() {
        return getSession();
    }

    public static void realeseSnmp(Snmp paramSnmp) {
    }

    /**
     * 
     * @param paramSNMPTarget
     * @param paramBoolean
     * @return
     */
    public static CommunityTarget getTarget(SNMPTarget paramSNMPTarget, boolean paramBoolean) {
        CommunityTarget localCommunityTarget = new CommunityTarget();
        localCommunityTarget.setCommunity(new OctetString(paramSNMPTarget.readCommunity));
        if (paramBoolean) {
            localCommunityTarget.setCommunity(new OctetString(paramSNMPTarget.writeCommunity));
        }
        Address localAddress = GenericAddress.parse("udp:" + paramSNMPTarget.nodeIP + "/" + paramSNMPTarget.port);
        localCommunityTarget.setAddress(localAddress);
        localCommunityTarget.setRetries(SNMPSysConfig.SnmpRetry);
        localCommunityTarget.setTimeout(SNMPSysConfig.SnmpTimeout);
        localCommunityTarget.setVersion(paramSNMPTarget.snmpVersion);
        return localCommunityTarget;
    }

    public static void main(String[] paramArrayOfString) {
        SNMPTarget localSNMPTarget = new SNMPTarget();
        localSNMPTarget.nodeIP = "192.168.2.188";
        localSNMPTarget.readCommunity = "public";
        localSNMPTarget.port = 166;
        try {
            MibLoader localMibLoader = new MibLoader();
            localMibLoader.addDir(new File("config/mib"));
            localMibLoader.load("HOST-RESOURCES-MIB");
            localMibLoader.load("OSPF-MIB");
            localMibLoader.load("RFC1213-MIB");
            Mib[] arrayOfMib = localMibLoader.getAllMibs();
            MibValueSymbol localMibValueSymbol = null;
            for (int i = 0; i < arrayOfMib.length; i++) {
                localMibValueSymbol = (MibValueSymbol) arrayOfMib[i].getSymbol("system");
                if (localMibValueSymbol != null)
                    break;
            }
            if (localMibValueSymbol == null) {
                throw new MyException("没有找到ifTable的mib定义");
            }
            String[] arrys = new String[] { "1.3.6.1.2.1.25.4.2.1.1", "1.3.6.1.2.1.25.4.2.1.2", "1.3.6.1.2.1.25.4.2.1.3", "1.3.6.1.2.1.25.4.2.1.4", "1.3.6.1.2.1.25.4.2.1.5", "1.3.6.1.2.1.25.4.2.1.6", "1.3.6.1.2.1.25.4.2.1.7" };
        } catch (Exception localException) {
            localException.printStackTrace();
            System.out.println(localException.getMessage());
        }
    }
}